﻿import QtQuick 2.0
import QtQuick.Window 2.12
import "../components"

Item {

    id: root

    property bool mutex: false

    function refresh(){
        // 新建模式：所有信息都清空
        if(gPageModel === 1){
            if(gIsNewOpenRefresh_expInfo){              // 从wlk文件打开实验
                gIsNewOpenRefresh_expInfo = false
                gComboModel_program.splice(0, 0, ".wlk扩增程序")
                if(gComboModel_kit.indexOf(gExperimentInfo["TestKit"]) !== -1){
                    gComboModel_kit.splice(gComboModel_kit.indexOf(gExperimentInfo["TestKit"]), 1)
                }if(gComboModel_system.indexOf(gExperimentInfo["System"]) !== -1){
                    gComboModel_system.splice(gComboModel_system.indexOf(gExperimentInfo["System"]), 1)
                }if(gComboModel_consume.indexOf(gExperimentInfo["ConsumableType"]) !== -1){
                    gComboModel_consume.splice(gComboModel_consume.indexOf(gExperimentInfo["ConsumableType"]), 1)
                }
                gComboModel_kit.splice(0, 0, gExperimentInfo["TestKit"])
                gComboModel_system.splice(0, 0, gExperimentInfo["System"])
                gComboModel_consume.splice(0, 0, gExperimentInfo["ConsumableType"])

                mutex = true
                edit_program.model = gComboModel_program
                edit_kit.model = gComboModel_kit
                edit_system.model = gComboModel_system
                edit_consume.model = gComboModel_consume
                edit_name    .setText(gExperimentInfo["ExperimentName"])
                edit_code    .setText(gExperimentInfo["Code"])
                edit_kit.changeCurrentText(CSetUp.getDefaultData("试剂盒"))
                edit_system.changeCurrentText(CSetUp.getDefaultData("扩增体系(μl)"))
                edit_consume.changeCurrentText(CSetUp.getDefaultData("耗材类型"))
                mutex = false
                syncExpInfo()
                return
            }else if(gIsNewRefresh_expInfo){            // 新建实验
                gIsNewRefresh_expInfo = false
                mutex = true

                edit_program.model = gComboModel_program
                edit_kit.model = gComboModel_kit
                edit_system.model = gComboModel_system
                edit_consume.model = gComboModel_consume

                edit_name.setText(Qt.formatDateTime(new Date, "yyyyMMddhhmmss"))
                edit_program.changeCurrentIndex(gDefaultIdx_program)
                edit_code.setText("001")
                edit_kit.changeCurrentText(CSetUp.getDefaultData("试剂盒"))
                edit_system.changeCurrentText(CSetUp.getDefaultData("扩增体系(μl)"))
                edit_consume.changeCurrentText(CSetUp.getDefaultData("耗材类型"))
                mutex = false
                syncExpInfo()
            }


        }

        // 重看模式：从后端获取当前设备的信息
        if(gPageModel === 0){
            gExperimentInfo = CExpInfo.getExperimentInfo(gCurDeviceId)
            text_name    .setText(gExperimentInfo["ExperimentName"])
            text_program .setText(gExperimentInfo["AmplName"])
            text_code    .setText(gExperimentInfo["Code"])
            text_kit     .setText(gExperimentInfo["TestKit"])
            text_system  .setText(gExperimentInfo["System"])
            text_consume .setText(gExperimentInfo["ConsumableType"])
        }
    }

    // 同步用户输入到存储
    function syncExpInfo(){
        if(mutex) return
        var amplName = edit_program.getText()
        gExperimentInfo["AmplGuid"] = "0"
        var programList = CAmplProgram.getProgramInfo(0)
        for(let i = 0; i < programList.length; i ++){
            if(amplName === programList[i]["ProgramName"]){
                gExperimentInfo["AmplGuid"] = programList[i]["Id"]
            }
        }
        gExperimentInfo["ExperimentName"] = edit_name.getText()
        gExperimentInfo["Code"] =           edit_code.getText()
        gExperimentInfo["System"] =         edit_system.getText()
        gExperimentInfo["ConsumableType"] = edit_consume.getText()
        gExperimentInfo["TestKit"] =        edit_kit.getText()
    }


    anchors.fill: parent
    Item{                               // 新建模式
        visible: gPageModel === 1
        anchors.centerIn: parent
        height: 360*dp
        width: 380*dp
        Column{
            spacing: 15*dp
            LineEditor{
                id: edit_name
                title: "实验名称"
                required: true
                editWidth: 300*dp
                inputMaxLength: 20
                lengthVisible: true
                onTextChanged: {
                    syncExpInfo()
                }
                Keys.onTabPressed: {
                    if (event.modifiers === Qt.ControlModifier) {
                        // 按下Ctrl+Tab时，焦点切换到下一个元素
                        focus = false
                        edit_code.focus = true
                    } else {
                        // 按下Tab时，焦点切换到下一个元素
                        focus = false
                        edit_code.focus = true
                    }
                }
            }
            CustomComboBox{
                id: edit_program
                model: ["扩增程序1", "扩增程序2", "扩增程序3"]
                title: "扩增程序"
                editWidth: 300*dp
                onCurrentText: {
                    syncExpInfo()
                }
                PushButton{
                    title: "查看"
                    text_color: "#1677FF"
                    x: parent.width
                    width: 60*dp
                    onReleased: {
//                        mainwindow.mainPage = 4
//                        mainwindow.tabProgram(edit_program.getText())
                        editAmplProgram(edit_program.getText())
                    }
                }
            }
            LineEditor{
                id: edit_code
                title: "编码"
                editWidth: 300*dp
                inputMaxLength: 20
                onTextChanged: {
                    syncExpInfo()
                }
                Keys.onTabPressed: {
                    if (event.modifiers === Qt.ControlModifier) {
                        // 按下Ctrl+Tab时，焦点切换到下一个元素
                        focus = false
                        edit_name.focus = true
                    } else {
                        // 按下Tab时，焦点切换到下一个元素
                        focus = false
                        edit_name.focus = true
                    }
                }
            }
            CustomComboBox{
                id: edit_kit
                title: "试剂盒"
                editWidth: 300*dp
                model: ["试剂盒1", "试剂盒2"]
                onCurrentText: {
                    syncExpInfo()
                }
            }
            CustomComboBox{
                id: edit_system
                title: "体系(μl)"
                editWidth: 300*dp
                model: ["10"]
                onCurrentText: {
                    syncExpInfo()
                }
            }
            CustomComboBox{
                id: edit_consume
                title: "耗材"
                model: ["耗材1", "耗材2"]
                editWidth: 300*dp
                onCurrentText: {
                    syncExpInfo()
                }
            }
        }
    }


    Item{                               // 查看模式
        visible: gPageModel === 0
        anchors.centerIn: parent
        height: 360*dp
        width: 380*dp
        Column{
            spacing: 15*dp
            LineEditor{
                id: text_name
                title: "实验名称"
                required: true
                editWidth: 300*dp
                editable: false
            }
            LineEditor{
                id: text_program
                title: "扩增程序"
                editWidth: 300*dp
                editable: false
            }
            LineEditor{
                id: text_code
                title: "编码"
                editWidth: 300*dp
                editable: false
            }
            LineEditor{
                id: text_kit
                title: "试剂盒"
                editWidth: 300*dp
                editable: false
            }
            LineEditor{
                id: text_system
                title: "体系(μl)"
                editWidth: 300*dp
                editable: false
            }
            LineEditor{
                id: text_consume
                title: "耗材"
                editWidth: 300*dp
                editable: false
            }
        }
    }
}
